﻿using DataAccess;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NLog;

namespace QuanLyKhachSan
{
    public partial class FormChiTietHoaDonThuePhong : Form
    {
        ChiTietHoaDonThuePhongManager controller = new ChiTietHoaDonThuePhongManager();
        protected static Logger _logger = LogManager.GetCurrentClassLogger();
        BindingSource bs = new BindingSource();
        int mahd=0;
        public FormChiTietHoaDonThuePhong(int mahd)
        {
            InitializeComponent();
            this.mahd = mahd;
        }

        private void FormChiTietHoaDonThuePhong_Load(object sender, EventArgs e)
        {
            Loading();
        }

        private void Loading()
        {
            var aResult = controller.TimChiTietTheoDK(cthd => cthd.MaHD == mahd);
            if (!aResult.Exceptions.Any())
            {
                bs.DataSource = aResult.Result;
                dgvChiTietHoaDon.DataSource = bs;
                for (int i = 3; i < dgvChiTietHoaDon.Columns.Count; i++)
                {
                    dgvChiTietHoaDon.Columns[i].Visible = false;
                }
            }
            else
            {
                string s = "";
                foreach (var ex in aResult.Exceptions)
                {
                    s += ex.Message + "\n";
                }
                MessageBox.Show(s);
                _logger.Error(s);
            }
        }

        private void dgvChiTietHoaDon_Click(object sender, EventArgs e)
        {
            int selectedRow = TimDong();
            
            if (selectedRow>=0)
            {
                string makh = dgvChiTietHoaDon.Rows[selectedRow].Cells[1].Value+"";
                string mahd = dgvChiTietHoaDon.Rows[selectedRow].Cells[0].Value+"";
                string sophong = dgvChiTietHoaDon.Rows[selectedRow].Cells[2].Value+"";
                ChiTietHoaDonThuePhong cthdp = controller.TimChiTietTheoDK(cthd=>cthd.MaKH+""==makh&&cthd.MaHD+""==mahd&&cthd.SoPhong+""==sophong).Result.Single();
                toolStripStatusLabel2.Text = dgvChiTietHoaDon.Rows[selectedRow].Cells[1].Value+" - " + (dgvChiTietHoaDon.Rows[selectedRow].Cells[2].Value+"").Trim()+" - "+cthdp.KhachHang.HoTenKH;
            }
        }

        private int TimDong()
        {
            for (int i = 0; i < dgvChiTietHoaDon.Rows.Count; i++)
            {
                if (dgvChiTietHoaDon.Rows[i].Selected)
                {
                    return i;
                }
            }
            return -1;
        }

        private int TimDong(string ma)
        {
            string[] s = ma.Split('-');
            for (int i = 0; i < dgvChiTietHoaDon.Rows.Count; i++)
            {
                if ((dgvChiTietHoaDon.Rows[i].Cells[1].Value + "").Trim() == s[0].Trim() && (dgvChiTietHoaDon.Rows[i].Cells[2].Value + "").Trim() == s[1].Trim())
                {
                    return i;
                }
            }
            return -1;
        }

        private void ibtnThem_Click(object sender, EventArgs e)
        {
            bool them = true;
            frmThongTinChiTietHoaDonThuePhong frm = new frmThongTinChiTietHoaDonThuePhong(mahd, them);
            frm.ShowDialog();
            if (frm.cthdtp!=null&&frm.themthanhcong)
            {
                if (controller.TimChiTietTheoDK(cthd=>cthd.MaHD==mahd).Result.Count<=1)
                {
                    Loading();
                }
                else
                {
                    bs.Add(frm.cthdtp);
                    dgvChiTietHoaDon.Rows[TimDong((frm.cthdtp.MaKH + " - " + frm.cthdtp.SoPhong))].Selected = true;
                }
            }
        }

        private void ibtnXoa_Click(object sender, EventArgs e)
        {
            DialogResult kq = MessageBox.Show("Bạn có muốn xóa dữ liệu này không?", "CẢNH BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (kq == DialogResult.Yes)
            {
                var aResult = controller.Xoa((ChiTietHoaDonThuePhong)bs[TimDong(toolStripStatusLabel2.Text)]);
                if (!aResult.Exceptions.Any())
                {
                    MessageBox.Show("Xóa thành công");
                    bs.RemoveAt(TimDong(toolStripStatusLabel2.Text));
                    _logger.Info("Xóa thành công");
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    MessageBox.Show(s);
                    _logger.Error(s);
                }
            }
        }


        private void themCTHDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ibtnThem.PerformClick();
        }

        private void XoaCTHDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ibtnXoa.PerformClick();
        }


    }
}
